<template>
  <div class="article-detail">
    <div class="article-header">
      <button class="back-btn" @click="$emit('back')">
        <i class="fas fa-arrow-left"></i>
        <span>返回列表</span>
      </button>
      <div class="article-meta">
        <span class="article-tag">{{ article.tag }}</span>
        <span class="article-date">
          <i class="far fa-calendar"></i>
          {{ article.date }}
        </span>
        <span class="article-time">
          <i class="far fa-clock"></i>
          {{ article.readTime }} 分钟阅读
        </span>
      </div>
    </div>

    <article class="article-content">
      <h1 class="article-title">{{ article.title }}</h1>
      <div class="article-body">
        <p class="article-excerpt">{{ article.excerpt }}</p>
        <div class="article-text" v-html="article.content"></div>
      </div>
    </article>

    <div class="article-footer">
      <div class="article-tags">
        <span v-for="(tag, index) in article.tags" 
              :key="index" 
              class="tag"
              @click="handleTagClick(tag)">
          #{{ tag }}
        </span>
      </div>
      <div class="article-actions">
        <button class="action-btn" @click="handleLike(article.id)">
          <i class="far fa-heart"></i>
          <span>{{ article.likes || 0 }}</span>
        </button>
        <button class="action-btn" @click="handleShare">
          <i class="fas fa-share-alt"></i>
          <span>分享</span>
        </button>
      </div>
    </div>

    <!-- 评论区 -->
    <div class="comments-section">
      <h3 class="comments-title">
        <i class="far fa-comments"></i>
        评论 ({{ comments.length }})
      </h3>

      <!-- 评论输入框 -->
      <div class="comment-form">
        <div class="user-avatar">
          <img v-if="currentUser?.avatar" :src="currentUser.avatar" :alt="currentUser.username">
          <div v-else class="avatar-placeholder">
            {{ currentUser?.username?.charAt(0).toUpperCase() || '?' }}
          </div>
        </div>
        <div class="comment-input-wrapper">
          <textarea 
            v-model="newComment" 
            placeholder="写下你的评论..."
            @focus="handleCommentFocus"
          ></textarea>
          <div class="comment-actions">
            <button 
              class="submit-comment" 
              :disabled="!newComment.trim()"
              @click="submitComment"
            >
              发表评论
            </button>
            <p v-if="!isLoggedIn" class="login-tip">
              请 <a href="#" @click.prevent="$emit('change-page', 'login')">登录</a> 后发表评论
            </p>
          </div>
        </div>
      </div>

      <!-- 评论列表 -->
      <div class="comments-list">
        <div v-for="comment in comments" 
             :key="comment.id" 
             class="comment-item">
          <div class="comment-avatar">
            <img v-if="comment.user.avatar" :src="comment.user.avatar" :alt="comment.user.username">
            <div v-else class="avatar-placeholder">
              {{ comment.user.username.charAt(0).toUpperCase() }}
            </div>
          </div>
          <div class="comment-content">
            <div class="comment-header">
              <span class="comment-username">{{ comment.user.username }}</span>
              <span class="comment-time">{{ comment.time }}</span>
            </div>
            <p class="comment-text">{{ comment.content }}</p>
            <div class="comment-footer">
              <button class="comment-action" @click="handleCommentLike(comment)">
                <i :class="comment.isLiked ? 'fas fa-heart' : 'far fa-heart'"></i>
                <span>{{ comment.likes }}</span>
              </button>
              <button class="comment-action" @click="showReplyInput(comment)">
                <i class="far fa-comment"></i>
                <span>回复</span>
              </button>
            </div>

            <!-- 回复输入框 -->
            <div v-if="activeReplyTo === comment.id" class="reply-form">
              <div class="user-avatar">
                <img v-if="currentUser?.avatar" :src="currentUser.avatar" :alt="currentUser.username">
                <div v-else class="avatar-placeholder">
                  {{ currentUser?.username?.charAt(0).toUpperCase() || '?' }}
                </div>
              </div>
              <div class="comment-input-wrapper">
                <textarea 
                  v-model="replyContent" 
                  :placeholder="`回复 ${comment.user.username}...`"
                  @focus="handleCommentFocus"
                ></textarea>
                <div class="comment-actions">
                  <button 
                    class="submit-comment" 
                    :disabled="!replyContent.trim()"
                    @click="submitReply(comment)"
                  >
                    发表回复
                  </button>
                  <button class="cancel-reply" @click="cancelReply">
                    取消
                  </button>
                </div>
              </div>
            </div>

            <!-- 回复列表 -->
            <div v-if="comment.replies && comment.replies.length > 0" class="replies-list">
              <div v-for="reply in comment.replies" 
                   :key="reply.id" 
                   class="reply-item">
                <div class="reply-avatar">
                  <img v-if="reply.user.avatar" :src="reply.user.avatar" :alt="reply.user.username">
                  <div v-else class="avatar-placeholder">
                    {{ reply.user.username.charAt(0).toUpperCase() }}
                  </div>
                </div>
                <div class="reply-content">
                  <div class="reply-header">
                    <span class="reply-username">{{ reply.user.username }}</span>
                    <span class="reply-time">{{ reply.time }}</span>
                  </div>
                  <p class="reply-text">
                    <template v-if="reply.replyTo">
                      回复 <span class="reply-to">@{{ reply.replyTo.username }}</span>：
                    </template>
                    {{ reply.content }}
                  </p>
                  <div class="reply-footer">
                    <button class="comment-action" @click="handleReplyLike(reply)">
                      <i :class="reply.isLiked ? 'fas fa-heart' : 'far fa-heart'"></i>
                      <span>{{ reply.likes }}</span>
                    </button>
                    <button class="comment-action" @click="showReplyInput(comment, reply)">
                      <i class="far fa-comment"></i>
                      <span>回复</span>
                    </button>
                  </div>

                  <!-- 回复输入框 -->
                  <div v-if="activeReplyTo === reply.id" class="reply-form">
                    <div class="user-avatar">
                      <img v-if="currentUser?.avatar" :src="currentUser.avatar" :alt="currentUser.username">
                      <div v-else class="avatar-placeholder">
                        {{ currentUser?.username?.charAt(0).toUpperCase() || '?' }}
                      </div>
                    </div>
                    <div class="comment-input-wrapper">
                      <textarea 
                        v-model="replyContent" 
                        :placeholder="`回复 ${reply.user.username}...`"
                        @focus="handleCommentFocus"
                      ></textarea>
                      <div class="comment-actions">
                        <button 
                          class="submit-comment" 
                          :disabled="!replyContent.trim()"
                          @click="submitReply(comment, reply)"
                        >
                          发表回复
                        </button>
                        <button class="cancel-reply" @click="cancelReply">
                          取消
                        </button>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ArticleDetail',
  props: {
    article: {
      type: Object,
      required: true
    },
    user: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      newComment: '',
      replyContent: '',
      activeReplyTo: null,
      comments: [
        {
          id: 1,
          user: {
            username: '张三',
            avatar: null
          },
          content: '这篇文章写得很好，对我帮助很大！',
          time: '2024-01-15 14:30',
          likes: 5,
          isLiked: false,
          replies: [
            {
              id: 11,
              user: {
                username: '李四',
                avatar: null
              },
              content: '同意，特别是关于Vue3的部分讲解得很清晰。',
              time: '2024-01-15 15:00',
              likes: 2,
              isLiked: false,
              replyTo: null
            },
            {
              id: 111,
              user: {
                username: '王五',
                avatar: null
              },
              content: '是的，组合式API确实让代码组织更清晰了。',
              time: '2024-01-15 15:30',
              likes: 1,
              isLiked: false,
              replyTo: {
                username: '李四'
              }
            }
          ]
        },
        {
          id: 1,
          user: {
            username: '张三',
            avatar: null
          },
          content: '这篇文章写得很好，对我帮助很大！',
          time: '2024-01-15 14:30',
          likes: 5,
          isLiked: false,
          replies: [
            {
              id: 11,
              user: {
                username: '李四',
                avatar: null
              },
              content: '同意，特别是关于Vue3的部分讲解得很清晰。',
              time: '2024-01-15 15:00',
              likes: 2,
              isLiked: false,
              replyTo: null
            },
            {
              id: 111,
              user: {
                username: '王五',
                avatar: null
              },
              content: '是的，组合式API确实让代码组织更清晰了。',
              time: '2024-01-15 15:30',
              likes: 1,
              isLiked: false,
              replyTo: {
                username: '李四'
              }
            }
          ]
        }
      ]
    }
  },
  computed: {
    isLoggedIn() {
      return !!this.user
    },
    currentUser() {
      return this.user
    }
  },
  methods: {
    handleTagClick(tag) {
      console.log('Tag clicked:', tag)
    },
    handleLike(articleId) {
      this.$emit('like', articleId)
    },
    handleShare() {
      // 实现分享功能
      if (navigator.share) {
        navigator.share({
          title: this.article.title,
          text: this.article.excerpt,
          url: window.location.href
        })
      } else {
        // 复制链接到剪贴板
        navigator.clipboard.writeText(window.location.href)
        alert('链接已复制到剪贴板')
      }
    },
    handleCommentFocus() {
      if (!this.isLoggedIn) {
        this.$emit('change-page', 'login')
      }
    },
    submitComment() {
      if (!this.newComment.trim()) return
      if (!this.isLoggedIn) {
        this.$emit('change-page', 'login')
        return
      }

      const comment = {
        id: Date.now(),
        user: {
          username: this.currentUser.username,
          avatar: this.currentUser.avatar
        },
        content: this.newComment.trim(),
        time: new Date().toLocaleString(),
        likes: 0,
        isLiked: false,
        replies: []
      }

      this.comments.unshift(comment)
      this.newComment = ''
    },
    showReplyInput(comment, replyTo = null) {
      if (!this.isLoggedIn) {
        this.$emit('change-page', 'login')
        return
      }
      this.activeReplyTo = replyTo ? replyTo.id : comment.id
      this.replyContent = ''
    },
    cancelReply() {
      this.activeReplyTo = null
      this.replyContent = ''
    },
    submitReply(comment, replyTo = null) {
      if (!this.replyContent.trim()) return
      if (!this.isLoggedIn) {
        this.$emit('change-page', 'login')
        return
      }

      // 检查回复层级
      if (replyTo && replyTo.replyTo) {
        // 如果回复对象已经是一个回复（第二层），则不允许继续回复
        alert('最多支持两层回复')
        return
      }

      const reply = {
        id: Date.now(),
        user: {
          username: this.currentUser.username,
          avatar: this.currentUser.avatar
        },
        content: this.replyContent.trim(),
        time: new Date().toLocaleString(),
        likes: 0,
        isLiked: false,
        replyTo: replyTo ? {
          username: replyTo.user.username
        } : null,
        replies: [] // 保留 replies 数组但不再使用
      }

      if (replyTo) {
        // 添加到评论的回复列表，而不是嵌套回复
        if (!comment.replies) {
          comment.replies = []
        }
        comment.replies.push(reply)
      } else {
        // 添加到评论的回复列表
        if (!comment.replies) {
          comment.replies = []
        }
        comment.replies.push(reply)
      }

      this.replyContent = ''
      this.activeReplyTo = null
    },
    handleCommentLike(comment) {
      if (!this.isLoggedIn) {
        this.$emit('change-page', 'login')
        return
      }
      comment.isLiked = !comment.isLiked
      comment.likes += comment.isLiked ? 1 : -1
    },
    handleReplyLike(reply) {
      if (!this.isLoggedIn) {
        this.$emit('change-page', 'login')
        return
      }
      reply.isLiked = !reply.isLiked
      reply.likes += reply.isLiked ? 1 : -1
    }
  }
}
</script>

<style scoped>
.article-detail {
  background: var(--card-bg);
  border-radius: 24px;
  padding: 30px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.article-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
}

.back-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border: none;
  background: var(--primary-light);
  color: white;
  border-radius: 50px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: var(--transition);
}

.back-btn:hover {
  background: var(--primary);
  transform: translateX(-2px);
}

.article-meta {
  display: flex;
  align-items: center;
  gap: 15px;
  color: var(--gray);
  font-size: 0.9rem;
}

.article-tag {
  background: var(--primary-light);
  color: white;
  padding: 4px 12px;
  border-radius: 50px;
  font-size: 0.8rem;
}

.article-meta i {
  margin-right: 5px;
}

.article-content {
  max-width: 800px;
  margin: 0 auto;
}

.article-title {
  font-size: 2.5rem;
  color: var(--dark);
  margin-bottom: 20px;
  line-height: 1.3;
}

.article-body {
  color: var(--dark);
  line-height: 1.8;
  font-size: 1.1rem;
}

.article-excerpt {
  font-size: 1.2rem;
  color: var(--gray);
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
}

.article-text {
  margin-bottom: 40px;
}

.article-text p {
  margin-bottom: 20px;
}

.article-text h2 {
  font-size: 1.8rem;
  margin: 40px 0 20px;
  color: var(--dark);
}

.article-text h3 {
  font-size: 1.5rem;
  margin: 30px 0 15px;
  color: var(--dark);
}

.article-text img {
  max-width: 100%;
  border-radius: 12px;
  margin: 20px 0;
}

.article-text code {
  background: #f1f5f9;
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
}

.article-text pre {
  background: #1e293b;
  color: #e2e8f0;
  padding: 20px;
  border-radius: 12px;
  overflow-x: auto;
  margin: 20px 0;
}

.article-text pre code {
  background: none;
  padding: 0;
  color: inherit;
}

.article-footer {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.article-tags {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.tag {
  color: var(--primary);
  font-size: 0.9rem;
  cursor: pointer;
  transition: var(--transition);
}

.tag:hover {
  color: var(--primary-light);
}

.article-actions {
  display: flex;
  gap: 15px;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  background: var(--card-bg);
  color: var(--dark);
  border-radius: 50px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: var(--transition);
  border: 1px solid rgba(0, 0, 0, 0.1);
}

.action-btn:hover {
  background: var(--primary-light);
  color: white;
  transform: translateY(-2px);
}

@media (max-width: 900px) {
  .article-detail {
    padding: 20px;
  }

  .article-title {
    font-size: 2rem;
  }

  .article-body {
    font-size: 1rem;
  }

  .article-excerpt {
    font-size: 1.1rem;
  }
}

@media (max-width: 600px) {
  .article-detail {
    padding: 15px;
    border-radius: 0;
  }

  .article-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }

  .article-meta {
    flex-wrap: wrap;
    gap: 10px;
  }

  .article-title {
    font-size: 1.8rem;
  }

  .article-body {
    font-size: 0.95rem;
  }

  .article-excerpt {
    font-size: 1rem;
  }

  .article-footer {
    flex-direction: column;
    gap: 20px;
    align-items: flex-start;
  }

  .article-actions {
    width: 100%;
    justify-content: space-between;
  }

  .action-btn {
    flex: 1;
    justify-content: center;
  }
}

/* 评论区样式 */
.comments-section {
  margin-top: 40px;
  padding-top: 30px;
  border-top: 1px solid rgba(0, 0, 0, 0.1);
}

.comments-title {
  font-size: 1.5rem;
  color: var(--dark);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.comments-title i {
  color: var(--primary);
}

.comment-form {
  display: flex;
  gap: 15px;
  margin-bottom: 30px;
}

.user-avatar,
.comment-avatar,
.reply-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}

.user-avatar img,
.comment-avatar img,
.reply-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.avatar-placeholder {
  width: 100%;
  height: 100%;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 1.2rem;
}

.comment-input-wrapper {
  flex: 1;
}

.comment-input-wrapper textarea {
  width: 100%;
  min-height: 100px;
  padding: 12px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 12px;
  resize: vertical;
  font-size: 0.95rem;
  background: var(--card-bg);
  transition: var(--transition);
}

.comment-input-wrapper textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.comment-input-wrapper textarea:disabled {
  background: #f1f5f9;
  cursor: not-allowed;
}

.comment-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
}

.submit-comment {
  padding: 8px 20px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 50px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: var(--transition);
}

.submit-comment:hover:not(:disabled) {
  background: var(--primary-light);
  transform: translateY(-2px);
}

.submit-comment:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-tip {
  font-size: 0.9rem;
  color: var(--gray);
}

.login-tip a {
  color: var(--primary);
  text-decoration: none;
  font-weight: 500;
}

.login-tip a:hover {
  color: var(--primary-light);
}

.comments-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.comment-item {
  display: flex;
  gap: 15px;
}

.comment-content {
  flex: 1;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 5px;
}

.comment-username {
  font-weight: 600;
  color: var(--dark);
}

.comment-time {
  font-size: 0.85rem;
  color: var(--gray);
}

.comment-text {
  color: var(--dark);
  line-height: 1.6;
  margin-bottom: 10px;
}

.comment-footer {
  display: flex;
  gap: 15px;
}

.comment-action {
  display: flex;
  align-items: center;
  gap: 5px;
  background: none;
  border: none;
  color: var(--gray);
  font-size: 0.9rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: var(--transition);
}

.comment-action:hover {
  color: var(--primary);
  background: rgba(99, 102, 241, 0.1);
}

.comment-action i {
  font-size: 1rem;
}

.replies-list {
  margin-top: 15px;
  margin-left: 55px;
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.reply-item {
  display: flex;
  gap: 12px;
}

.reply-avatar {
  width: 32px;
  height: 32px;
}

.reply-content {
  flex: 1;
}

.reply-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.reply-username {
  font-weight: 500;
  color: var(--dark);
  font-size: 0.9rem;
}

.reply-time {
  font-size: 0.8rem;
  color: var(--gray);
}

.reply-text {
  color: var(--dark);
  line-height: 1.5;
  font-size: 0.9rem;
  margin-bottom: 8px;
}

.reply-footer {
  display: flex;
  gap: 12px;
}

.reply-footer .comment-action {
  font-size: 0.85rem;
}

.reply-form {
  display: flex;
  gap: 12px;
  margin-top: 15px;
  padding: 15px;
  background: rgba(0, 0, 0, 0.02);
  border-radius: 12px;
}

.reply-form .user-avatar {
  width: 32px;
  height: 32px;
}

.reply-form textarea {
  min-height: 60px;
  font-size: 0.9rem;
}

.cancel-reply {
  padding: 8px 16px;
  background: none;
  border: 1px solid var(--gray);
  color: var(--gray);
  border-radius: 50px;
  font-size: 0.9rem;
  cursor: pointer;
  transition: var(--transition);
}

.cancel-reply:hover {
  background: rgba(0, 0, 0, 0.05);
}

.nested-replies {
  display: none;
}

.reply-to {
  color: var(--primary);
  font-weight: 500;
}

@media (max-width: 600px) {
  .comments-section {
    margin-top: 30px;
    padding-top: 20px;
  }

  .comments-title {
    font-size: 1.3rem;
  }

  .comment-form {
    gap: 10px;
  }

  .user-avatar,
  .comment-avatar {
    width: 32px;
    height: 32px;
  }

  .comment-input-wrapper textarea {
    min-height: 80px;
    font-size: 0.9rem;
  }

  .submit-comment {
    padding: 6px 15px;
    font-size: 0.85rem;
  }

  .replies-list {
    margin-left: 42px;
  }

  .reply-avatar {
    width: 28px;
    height: 28px;
  }

  .reply-username {
    font-size: 0.85rem;
  }

  .reply-text {
    font-size: 0.85rem;
  }

  .reply-form {
    padding: 10px;
    gap: 8px;
  }

  .reply-form .user-avatar {
    width: 28px;
    height: 28px;
  }

  .reply-form textarea {
    min-height: 50px;
    font-size: 0.85rem;
  }

  .nested-replies {
    margin-left: 32px;
    padding-left: 10px;
  }
}
</style> 